#!/usr/bin/perl


###	This script is part of the Zeitcrawler v1.3 (http://code.google.com/p/zeitcrawler/).
###	Copyright (C) Adrien Barbaresi 2011-2013.
###	This is free software, released under the GNU GPL v3 license (http://www.gnu.org/licenses/gpl.html).

# Function: expects a file named "ZEIT_flatfile" and converts it to a (hopefully valid) XML file named "ZEIT_crawl.xml".
# Use: without arguments


use strict;
use warnings;

my $text;

my $output = "ZEIT_crawl.xml";
open (OUTPUT, ">", $output) or die "Can't open $output: $!";
print OUTPUT "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
print OUTPUT "<!--File generated by the zeitcrawler : http://code.google.com/p/zeitcrawler/-->\n";
print OUTPUT "<!DOCTYPE collection [
  <!ELEMENT collection (text+)>
  <!ELEMENT text (rohtext)>
  <!ATTLIST text titel CDATA #REQUIRED>
  <!ATTLIST text excerpt CDATA #REQUIRED>
  <!ATTLIST text autor CDATA #REQUIRED>
  <!ATTLIST text datum CDATA #REQUIRED>
  <!ATTLIST text url CDATA #REQUIRED>
  <!ELEMENT rohtext (#PCDATA)>
]>\n\n\n";
print OUTPUT "<collection>\n\n";


my $input = "ZEIT_flatfile";
open (INPUT, "<", $input) or die "Can't open $input: $!";
while (<INPUT>) {
	$text .= $_;
	if ($_ =~ m/^-----$/) {
		$text = xmlize($text);
		$text =~ s/Titel: (.*?)\nExcerpt: (.*?)\nAutor: (.*?)\nDatum: (.*?)\nurl: (.*?)\n\n(.+?)-----/<text titel="$1" excerpt="$2" autor="$3" datum="$4" url="$5">\n<rohtext>\n$6<\/rohtext>\n<\/text>\n\n/s;
		print OUTPUT $text;
		$text = ();
	}
}
close(INPUT);


print OUTPUT "</collection>";
close(OUTPUT);


sub xmlize {
	my $string = shift;
	$string =~ s/& /&amp;/g;
	$string =~ s/'/&apos;/g;
	$string =~ s/"/&quot;/g;
	return $string;
}
